package com.projectx.action.chat.DevoteActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.projectx.R;

import com.projectx.action.chat.GroupChatActivity;
import com.projectx.util.ActivityManage;
import com.projectx.util.MyAlertDialog;

import java.util.ArrayList;
import java.util.List;

public class DevoteActivity extends AppCompatActivity {
    List<String> content=new ArrayList<String>();
    List<String> time=new ArrayList<String>();
    List<Bitmap> bitmaps=new ArrayList<Bitmap>();
    List<Integer> status= new ArrayList<Integer>();
    List<String[]> choice=new ArrayList<String[]>();
    List<String[]> count=new ArrayList<String[]>();
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_devote);
        ActivityManage.addActivity("DevoteActivity",this);
        try {
            getSupportActionBar().hide(); //去除标题栏
        } catch (NullPointerException e){
            Log.d("KMH","没有状态栏标题");
        }
        content.add("内容");
        time.add("时间");
        status.add(2);
        String[] a={"选项A","选项B","选项C"};
        choice.add(a);
        String[]b={"1","2","3"};
        count.add(b);
        final RecyclerView recyclerView=findViewById(R.id.recycler_view);
        final ImageView back= findViewById(R.id.back);
        final ImageView add=findViewById(R.id.add);
        if (true){
            add.setVisibility(View.VISIBLE);
        }
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              //添加投票
                Intent intent=new Intent(DevoteActivity.this,AddDevoteActivity.class);
                startActivity(intent);
            }
        });
       back.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               ActivityManage.exitActivity("DevoteActivity");
           }
       });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new DevoteActivityAdapter(this, content,bitmaps,time,status,choice,count));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
    }

    @Override
    public void onBackPressed() {
        ActivityManage.exitActivity("DevoteActivity");
    }
}
class DevoteActivityAdapter extends RecyclerView.Adapter<DevoteActivityAdapter.VH> {
    private Context context;
    private List<String> data;
    private  List<Bitmap>bitmaps;
    private List<String> time;
    private List<Integer> status;
    private List<String[]> choice;
    private List<String[]> count;
    public DevoteActivityAdapter(Context context, List<String> data, List<Bitmap>bitmaps, List<String> time, List<Integer> status, List<String[]> choice, List<String[]> count) {
        this.context = context;
        this.data = data;
        this.bitmaps=bitmaps;
        this.time=time;
        this.status=status;
        this.count=count;
        this.choice=choice;
    }

    @NonNull
    @Override
    public DevoteActivityAdapter.VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_devote, parent, false);
        return new DevoteActivityAdapter.VH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DevoteActivityAdapter.VH holder, final int position) {
        holder.textView_content.setText(data.get(position));
        //holder.imageView.setImageBitmap(bitmaps.get(position));
        holder.textView_time.setText(time.get(position));
        if (status.get(position)==1){
            holder.status.setText("已投票");
        }
        else if (status.get(position)==2){
            holder.status.setText("未投票");
            holder.status.setTextColor(Color.parseColor("#FF0000"));
        }

        holder.click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             if (status.get(position)==2){
                 final MyAlertDialog myAlertDialog = new MyAlertDialog();
                 myAlertDialog.singleChoiceDialog(context, data.get(position),choice.get(position),0);
             }
             if (status.get(position)==1){
              //   final MyAlertDialog myAlertDialog = new MyAlertDialog();
                 View layout=View.inflate(context,R.layout.activity_devote_recycle,null);
                // View view=View.inflate(context,R.layout.activity_devote_recycle,null);
                 RecyclerView recyclerView=layout.findViewById(R.id.recycler_view);
                 recyclerView.setLayoutManager(new LinearLayoutManager(context));
                 recyclerView.setAdapter(new DevoteDialogActivityAdapter(context,choice.get(position),count.get(position)));
                 recyclerView.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
              //   myAlertDialog.DevoteDialog(context,layout,data.get(position));
                 AlertDialog.Builder alert = new AlertDialog.Builder(context);
                 alert.setTitle(data.get(position)).setView(layout);
                 alert.setPositiveButton("确认",null);   //添加点击事件
                 alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
                     @Override
                     public void onDismiss(DialogInterface dialog) {
                         dialog.dismiss();
                     }
                 });
                 alert.show();
             }
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public static class VH extends RecyclerView.ViewHolder {

        TextView textView_content,status;
        TextView textView_time;
        ImageView imageView;
        View click;
        public VH(@NonNull View itemView) {
            super(itemView);
            textView_content = itemView.findViewById(R.id.text);
            imageView= itemView.findViewById(R.id.imageView);
            click=itemView.findViewById(R.id.click);
            textView_time=itemView.findViewById(R.id.time);
            status=itemView.findViewById(R.id.status);
        }
    }
}
